<template>
    <ul class="list">
        <li class="item" v-for="item in Alphabet" :key="item"
            :ref="item"
            @click="handleLetterClick"
            @touchstart="handleTouchStart"
            @touchmove="handleTouchMove"
            @touchend="handleTouchEnd"

        >{{item}}
        </li>
    </ul>
</template>

<script>
    export default {
        name: "CityAlphabet",
        data(){
            return {
                touchStatus:false,
                startY:0,
                timer:null
            }
        },
        updated(){
            this.startY = this.$refs['A'][0].offsetTop;
        },
        props: {
            Alphabet: Array.isRequired
        },
        methods: {
            handleLetterClick(event) {
                this.$emit('change', event.target.innerText)
            },
            handleTouchStart() {
                this.touchStatus = true;
            },
            handleTouchMove(event) {
                if(this.touchStatus){
                    if(this.timer){
                        clearTimeout(this.timer)
                    }
                    this.timer = setTimeout(() => {
                        const touchY = event.touches[0].clientY - 74;
                        const index = Math.floor((touchY - this.startY)/ 20) ;
                        if (index>=0 && index< this.Alphabet.length) {
                            this.$emit('change',this.Alphabet[index]);
                        }
                    },16);
                }
            },
            handleTouchEnd() {
                this.touchStatus = false;
            }
        }
    }
</script>

<style lang="stylus" scoped>
    @import "~styles/varibles.styl"
    .list
        display flex
        flex-direction column
        justify-content center
        position absolute
        top 1.58rem
        right 0
        bottom 0
        width .4rem
        .item
            line-height .4rem
            text-align center
            color $bgColor
            touch-action none
</style>